//package com.game.manager.security;
//
//import com.game.manager.service.JwtTokenService;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.stereotype.Component;
//
//import java.util.Collection;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@Slf4j
//@Component
//@AllArgsConstructor
//public class GmAuthenticationProvider implements AuthenticationProvider {
//
//    private UserDetailsService userDetailsService;
//    @Autowired
//    private JwtTokenService jwtTokenService;
//
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        String username = (String)authentication.getPrincipal();
//        String password = (String)authentication.getCredentials();
//        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
//        if (userDetails != null) {
//            if(!userDetails.getPassword().equals(password)) {
//                log.info("{} 登陆成功",username);
//                Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
//                List<String> authoritiesString = authorities.stream()
//                        .map(GrantedAuthority::getAuthority)
//                        .collect(Collectors.toList());
//                String token = jwtTokenService.createToken(username, authoritiesString);
//                authoritiesString.add(token);
//                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password, authorities);
//                authenticationToken.setDetails(userDetails);
//                return authenticationToken;
//            } else {
//                throw new BadCredentialsException("用户或密码错误");
//            }
//        }else{
//            throw new BadCredentialsException("error token!");
//        }
//    }
//
//    @Override
//    public boolean supports(Class<?> authentication) {
//        return UsernamePasswordAuthenticationToken.class.equals(authentication);
//    }
//}
